package by.admonko.universaltesting.dataaccess.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import by.admonko.universaltesting.dataaccess.TestDao;
import by.admonko.universaltesting.datamodel.Test;
import by.admonko.universaltesting.datamodel.Test_;
import by.admonko.universaltesting.datamodel.UserGroup;
import by.admonko.universaltesting.datamodel.UserTbl;

@Repository
public class TestDaoImpl extends AbstractDaoImpl<Integer, Test> implements
		TestDao {

	protected TestDaoImpl() {
		super(Test.class);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<Test> getByUserGroup(Set<UserGroup> usGroup) {

		CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

		CriteriaQuery<Test> root = cBuilder.createQuery(Test.class);
		Root<Test> criteria = root.from(Test.class);

		root.select(criteria);

		root.where(cBuilder.equal(criteria.get(Test_.userGroup), usGroup));

		TypedQuery<Test> query = getEm().createQuery(root);
		List<Test> list = query.getResultList();
		return list;

	}

	@Override
	public List<Test> getByUserOwner(UserTbl user) {
		CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

		CriteriaQuery<Test> root = cBuilder.createQuery(Test.class);
		Root<Test> criteria = root.from(Test.class);

		root.select(criteria);

		root.where(cBuilder.equal(criteria.get(Test_.owner), user));

		TypedQuery<Test> query = getEm().createQuery(root);
		List<Test> list = query.getResultList();
		return list;
	}

}
